<template>
  <div class="whole wholeLogin">
    <div :class="back">
      <div class="backBut">
        <a-radio-group :value="size" @change="handleSizeChange">
          <a-radio-button value="large" @click="backButLucency">
            <img style="width:16px" :src="daytime" alt="">
          </a-radio-button>
          <a-radio-button value="default" @click="backButDark">
            <img style="width:16px" :src="night" alt="">
          </a-radio-button>
        </a-radio-group>
      </div>
      <div :class="center">
        <div class="logo">
          <img src="../../../images/login/logo.png" alt />
        </div>
        <div class="text">欢迎登录本系统</div>
        <div class="formCenter">
          <a-form
            id="components-form-demo-normal-login"
            :form="form"
            class="login-form"
            @submit="handleSubmit"
          >
            <a-form-item>
              <a-input
                class="loginUserName"
                v-decorator="['userName',{ rules: [{ required: true, message: '请输入您的用户名!' }] },]"
                placeholder="用户名"
              >
                <a-icon class="loginAicon" slot="prefix" type="user" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-input
                class="loginPassword"
                v-decorator="[
                'password',
                { rules: [{ required: true, message: '请输入您的密码!' }] },
              ]"
                type="password"
                placeholder="密码"
              >
                <a-icon class="loginAicon" slot="prefix" type="lock" />
              </a-input>
            </a-form-item>

            <a-form-item>
              <div class="client">
                <a-input
                  autocomplete="off"
                  class="clientCodeInp"
                  v-decorator="[
                  'clientCode',
                  { rules: [{ required: true, message: '请输入您的验证码!' }] },
                ]"
                  placeholder="验证码"
                >
                  <a-icon class="loginAicon" slot="prefix" type="calendar" />
                </a-input>

                <div class="clientCode" @click="changeCode()">
                  <img :src="imgPath" title="点击刷新" alt="点击刷新" />
                </div>
              </div>
            </a-form-item>

            <a-form-item :class="Abut">
              <a-button html-type="submit" class="login-form-button">登录</a-button>
            </a-form-item>
          </a-form>
          <div class="Loading" v-show="Loading">
            <a-spin tip="Loading..."></a-spin>
          </div>
        </div>
        <div :class="technical">技术支持: 北京航天长峰股份有限公司</div>
      </div>
    </div>
  </div>
</template>

<script>
import api from "../../api/login/login.js";
export default {
  beforeCreate() {
    this.form = this.$form.createForm(this, { name: "normal_login" });
  },
  data() {
    return {
      userInfo: {},
      imgPath: "",
      clientCode: "",
      Loading: false,
      size: "large",
      center: "center",
      Abut: "Abut",
      technical: "technical",
      back: "back",
      menuLeft: '',
      principal: '',
      derma:{
        menuLeft: '',
        principal: '',
      },
      daytime: '',
      night: '',
    };
  },
  watch: {
    center: function(newval, oldval) {
      window.sessionStorage.setItem("loginCenter", newval);
    },
    Abut: function(newval, oldval) {
      window.sessionStorage.setItem("loginAbut", newval);
    },
    technical: function(newval, oldval) {
      window.sessionStorage.setItem("loginTechnical", newval);
    },
    back: function(newval, oldval) {
      window.sessionStorage.setItem("loginBack", newval);
    }
  },
  mounted() {
    let _this = this;
    console.log(_this)
    this.clientCodeGain();
    _this.center = sessionStorage.getItem("loginCenter");
    _this.Abut = sessionStorage.getItem("loginAbut");
    _this.technical = sessionStorage.getItem("loginTechnical");
    _this.back = sessionStorage.getItem("loginBack");
    if (_this.center != "center1") {
      _this.size = "large";
    } else {
      _this.size = "default";
    }
    if (_this.size == "large") {
      _this.backButLucency();
    } else {
      _this.backButDark();
    }
  },
  methods: {
    handleSizeChange(e) {
      this.size = e.target.value;
    },
    backButLucency() {
      this.center = "center";
      this.Abut = "Abut";
      this.technical = "technical";
      this.back = "back";
      this.daytime = require("../../../images/home/daytime1.png");
      this.night = require("../../../images/home/night.png");
    },
    backButDark() {
      this.center = "center1";
      this.Abut = "Abut1";
      this.technical = "technical1";
      this.back = "back1";
      this.daytime = require("../../../images/home/daytime.png");
      this.night = require("../../../images/home/night1.png");
    },
    // 获取验证码
    clientCodeGain() {
      // 随机生成四位数
      function rand(min, max) {
        return Math.floor(Math.random() * (max - min)) + min;
      }

      let randNum = rand(1000, 9999);
      this.clientCode = randNum;
      api.validateCode(null, { clientCode: randNum }).then(res => {
        this.imgPath = window.URL.createObjectURL(res);
      });
    },
    // 点击验证码刷新验证码
    changeCode() {
      this.clientCodeGain();
    },
    // 登录
    handleSubmit(e) {
      let _this = this;
      e.preventDefault();
      _this.form.validateFields((err, values) => {
        if (!err) {
          _this.Loading = true;
          _this.userInfo = values;
          _this.userInfo.code = values.clientCode;
          _this.userInfo.clientCode = _this.clientCode;
          api.login(null, _this.userInfo).then(res => {
            if (res.code == 200) {
              _this.Loading = false;
              let user = {};
              user.userName = _this.userInfo.userName;
              user.password = _this.userInfo.password;
              window.sessionStorage.setItem("token", res.data.token);
              _this.$message.success("登录成功");
              _this.getUserInfo();
            } else {
              _this.Loading = false;
              if(res.message != "验证码错误") {
                _this.$message.error("用户名或密码错误");
              }else {
                _this.$message.error(res.message);
              }
              // 重置
              _this.form.resetFields();
              // 刷新验证码
              _this.clientCodeGain();
            }
          });
        }
      });
    },
    getUserInfo() {
      let _this = this;
      api.userInfo().then(res => {
        if (res.code == 200) {
          _this.userInfo = res.data;
          _this.userInfo = JSON.stringify(_this.userInfo);
          window.sessionStorage.setItem("userInfo", _this.userInfo);
          //window.sessionStorage.setItem("derma", _this.size);
          //_this.$router.push("/home");
          if(_this.size == 'large') {
            _this.derma.menuLeft = 'menuLeft',
            _this.derma.principal = 'principal'
          }else if(_this.size == 'default') {
            _this.derma.menuLeft = 'menuDeaDolour',
            _this.derma.principal = 'prinDeaDolour'
          }
          _this.$router.push({
            name: 'Welcome', //注意使用 params 时一定不能使用 path
            params: _this.derma,
          })
          location.reload();
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
#components-form-demo-normal-login .login-form {
  max-width: 300px;
}

#components-form-demo-normal-login .login-form-forgot {
  float: right;
}

#components-form-demo-normal-login .login-form-button {
  width: 100%;
}

.whole {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-items: center;
  background-size: cover;
  background-size: 100% 100%;
  position: relative;
  .back {
    width: 100%;
    height: 100vh;
    display: flex;
    justify-items: center;
    background-image: url(../../../images/login/bg-back.png);
    background-size: cover;
    background-size: 100% 100%;
    position: relative;
  }
  .back1 {
    width: 100%;
    height: 100vh;
    display: flex;
    justify-items: center;
    background-image: url(../../../images/login/bg-back1.png);
    background-size: cover;
    background-size: 100% 100%;
    position: relative;
  }
  .Loading {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .center {
    margin: auto;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 959px;
    height: 599px;
    background-image: url(../../../images/login/bg-input.png);
    background-size: cover;
    background-size: 100% 100%;
    position: relative;
    .logo {
      margin-left: 98px;
      margin-top: 97px;
    }
    .text {
      font-size: 35px;
      margin-left: 98px;
      margin-top: 40px;
    }
    .formCenter {
      margin-left: 98px;
      width: 300px;
      height: 350px;
      display: flex;
      align-items: center;
      position: relative;
      #components-form-demo-normal-login {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        height: 200px;
        width: 90%;
      }
    }
  }
  .center1 {
    margin: auto;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 959px;
    height: 599px;
    background-image: url(../../../images/login/bg-input1.png);
    background-size: cover;
    background-size: 100% 100%;
    position: relative;
    .logo {
      margin-left: 98px;
      margin-top: 97px;
    }
    .text {
      font-size: 35px;
      margin-left: 98px;
      margin-top: 40px;
    }
    .formCenter {
      margin-left: 98px;
      width: 300px;
      height: 350px;
      display: flex;
      align-items: center;
      position: relative;
      #components-form-demo-normal-login {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        height: 200px;
        width: 90%;
      }
    }
  }
  .technical {
    width: 300px;
    position: absolute;
    bottom: -52px;
    left: 50%;
    transform: translate(-150px);
    color: #0078b6;
    font-size: 16px;
  }
  .technical1 {
    width: 300px;
    position: absolute;
    bottom: -52px;
    left: 50%;
    transform: translate(-150px);
    color: #464f4e;
    font-size: 16px;
  }

  .client {
    display: flex;
    position: relative;
    .clientCode {
      width: 100px;
      height: 100%;
      position: absolute;
      left: 210px;
      display: flex;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .Abut {
    width: 215px;
    height: 48px;
    .login-form-button {
      width: 215px;
      height: 48px;
      border-radius: 48px;
      box-shadow: 0px 0px 20px 0px rgba(0, 120, 182, 0.6);
      background-color: #0078b5;
      border: none;
      color: #fff;
      font-size: 18px;
    }
  }
  .Abut1 {
    width: 215px;
    height: 48px;
    .login-form-button {
      width: 215px;
      height: 48px;
      border-radius: 48px;
      box-shadow: 0px 0px 20px 0px rgba(0, 120, 182, 0.6);
      background-color: #2091bd;
      border: none;
      color: #fff;
      font-size: 18px;
    }
  }
}
</style>
<style>
.wholeLogin .ant-input-affix-wrapper .ant-input {
  width: 300px !important;
  height: 48px !important;
  border-radius: 24px !important;
}

.wholeLogin .center .loginAicon {
  color: #5e78ab !important;
  font-size: 16px !important;
}
.wholeLogin .center1 .loginAicon {
  color: #2095b6 !important;
  font-size: 16px !important;
}

.wholeLogin .clientCodeInp.ant-input-affix-wrapper .ant-input {
  width: 200px !important;
}
</style>
